package com.clz.workorder.fragment

import com.alibaba.android.arouter.facade.annotation.Route
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.blankj.utilcode.util.ObjectUtils
import com.clz.workorder.BR
import com.clz.workorder.R
import com.clz.workorder.databinding.FragmentTransferOrderBinding
import com.clz.workorder.viewmodel.TransferOrderViewModel
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.config.OrderType
import com.czl.base.data.bean.OrderTypeBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper

/**
 *
 * @Description:转单
 * @Author: XCH
 * @CreateDate: 2021/12/15 10:06
 */
@Route(path = AppConstants.Router.Order.F_TRANSFER_ORDER)
class TransferOrderFragment : BaseFragment<FragmentTransferOrderBinding, TransferOrderViewModel>() {

    private var orderTypeList: List<OrderTypeBean>? = null
    private var orderTypeBean: OrderTypeBean? = null

    override fun initContentView(): Int {
        return R.layout.fragment_transfer_order
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("转单")
        if (arguments != null) {
            viewModel.no = requireArguments().getString(AppConstants.BundleKey.ORDER_NO_KEY, "")
            viewModel.repairType =
                requireArguments().getString(AppConstants.BundleKey.REPAIR_TYPE, "")
            viewModel.subTypeId =
                requireArguments().getString(AppConstants.BundleKey.SUB_TYPE_ID, "")
            viewModel.queryWorkOrderType()
        }
    }

    override fun initViewObservable() {
        viewModel.uc.queryTypeCompleteEvent.observe(this, { list ->
            orderTypeBean =
                Stream.of(list).filter { num -> num.id == viewModel.subTypeId }.findFirst().get()
            orderTypeBean?.let {
                viewModel.typeName.set(it.name)
                viewModel.deptName.set(it.deptName)
                viewModel.subTypeId = it.id
                viewModel.deptId = it.deptId
            }
            orderTypeList = list
        })

        viewModel.uc.openTypePopEvent.observe(this, {
            showOrderTypePop()
        })
        viewModel.uc.openUserPopEvent.observe(this, {
            viewModel.getUsersByDeptId()
        })
        viewModel.uc.queryUserCompleteEvent.observe(this, {
            if (ObjectUtils.isEmpty(it)) {
                showNormalToast("维修人列表为空！")
                return@observe
            }
            DialogHelper.showBottomListDialog(
                requireContext(),
                Stream.of(it).map { t -> t.userName }.withoutNulls()
                    .collect(Collectors.toList()) as ArrayList<String>
            ) { position, text ->
                viewModel.repairerName.set(text)
                viewModel.repairerId = it[position].userId
            }
        })
        viewModel.uc.turnOrderEvent.observe(this, {
            LiveBusCenter.postTakeSuccessEvent(OrderType.TAKE_ORDER)
            back()
        })
    }

    private fun showOrderTypePop() {
        if (ObjectUtils.isEmpty(orderTypeList)) {
            showNormalToast("报修类型列表为空！")
            return
        }
        orderTypeList?.let {
            DialogHelper.showBottomListDialog(
                requireContext(),
                Stream.of(it).map { t -> t.name }.withoutNulls()
                    .collect(Collectors.toList()) as ArrayList<String>
            ) { position, text ->
                viewModel.typeName.set(text)
                viewModel.deptName.set(it[position].deptName)
                viewModel.subTypeId = it[position].id
                viewModel.deptId = it[position].deptId
                viewModel.repairerName.set("")
                viewModel.repairerId = ""
            }
        }
    }
}